package com.whmnx.chitchat.ui.book;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.microsoft.signalr.HubConnection;
import com.tencent.imsdk.TIMConversationType;
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo;
import com.whmnx.chitchat.R;
import com.whmnx.chitchat.adapter.UserAdpter;
import com.whmnx.chitchat.adapter.UserAdpter2;
import com.whmnx.chitchat.adapter.UserAdpter3;
import com.whmnx.chitchat.base.BaseActivity;
import com.whmnx.chitchat.bean.UserBean;
import com.whmnx.chitchat.constants.Constants;
import com.whmnx.chitchat.event.EventConstants;
import com.whmnx.chitchat.event.MessageEvent;
import com.whmnx.chitchat.http.CallBack;
import com.whmnx.chitchat.http.HttpClient;
import com.whmnx.chitchat.ui.chat.AddGroupActivity;
import com.whmnx.chitchat.ui.chat.ChatActivity;
import com.whmnx.chitchat.ui.my.FabuActivity;
import com.whmnx.chitchat.ui.my.MyActivity;
import com.whmnx.chitchat.utils.KeyboardUtils;
import com.whmnx.chitchat.utils.SPUtil;
import com.whmnx.chitchat.utils.UItils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2020/7/23 0023 9:24
 * version: 1.0
 *
 * @author Administrator
 */
public class ChuangFriendsActivity extends BaseActivity {
    @BindView(R.id.rl_user)
    RecyclerView rlUser;
    @BindView(R.id.tv_num)
    TextView tvNum;
    @BindView(R.id.message_search)
    EditText query;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    private UserAdpter3 userAdpter;

    private HubConnection hubConnection;
    private boolean isConnect;//是否连接成功

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.chuang_friends_activity);
        setTitle("常用好友");
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.getMessageKey() != null) {
            if (messageEvent.getMessageKey().equals(EventConstants.FRIENDS)) {
                loadData();
            }
        }
    }

    @Override
    protected void loadData() {
        rlUser.setLayoutManager(new LinearLayoutManager(this));
        rlUser.setItemAnimator(new DefaultItemAnimator());
        userAdpter = new UserAdpter3(this);
        rlUser.setAdapter(userAdpter);

        Map<String, Object> params = new HashMap<>();
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Type", "2");
        HttpClient.get(ChuangFriendsActivity.this, Constants.GETFRIENDLIST, params, new CallBack<List<UserBean>>() {
            @Override
            public void onSuccess(List<UserBean> result) {
                userAdpter.addAll(result);
                tvNum.setText("常用好友(" + result.size() + ")");
            }
        });


        query.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Map<String, Object> params = new HashMap<>();
                params.put("Con", s.toString());
                params.put("UserID", SPUtil.getString(Constants.USERID));
                params.put("Type", "2");
                HttpClient.get(ChuangFriendsActivity.this, Constants.GETSEATCHFRIENDLIST, params, new CallBack<List<UserBean>>() {
                    @Override
                    public void onSuccess(List<UserBean> result) {
                        userAdpter.replaceAll(result, true);
                    }
                });
            }

            @Override
            public void afterTextChanged(Editable s) {
                KeyboardUtils.showKeyBoard(ChuangFriendsActivity.this, getCurrentFocus());
                tvCancel.setVisibility(View.VISIBLE);
            }
        });

        userAdpter.setOnItemClickListener((viewHolder, view, i) -> {
            SPUtil.put(Constants.FORMPHOTO, userAdpter.getItem(i).getHeadImg());
            ChatInfo chatInfo = new ChatInfo();
            chatInfo.setType(TIMConversationType.C2C);
            chatInfo.setId(userAdpter.getItem(i).UserID);
            chatInfo.setChatName(userAdpter.getItem(i).NickName);
            int type = getIntent().getIntExtra("type", 0);
            if (type == 1) {
                chatInfo.setTopChat(true);
            } else {
                chatInfo.setTopChat(false);
            }
            Intent intent = new Intent(ChuangFriendsActivity.this, ChatActivity.class);
            intent.putExtra(Constants.CHAT_INFO, chatInfo);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        });
    }


    private void search(String trim) {
        Map<String, Object> params = new HashMap<>();
        params.put("Con", trim);
        params.put("UserID", SPUtil.getString(Constants.USERID));
        params.put("Type", "2");
        HttpClient.get(ChuangFriendsActivity.this, Constants.GETSEATCHFRIENDLIST, params, new CallBack<List<UserBean>>() {
            @Override
            public void onSuccess(List<UserBean> result) {
                userAdpter.replaceAll(result, true);
            }
        });
    }


    @OnClick({R.id.tv_add, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_add:
                startActivity(new Intent(this, AddGroupActivity.class).putExtra("type", 1));
                break;
            case R.id.tv_cancel:
                query.setText("");
                tvCancel.setVisibility(View.GONE);
                KeyboardUtils.hideKeyBoard(ChuangFriendsActivity.this, getCurrentFocus());
                break;
            default:
        }
    }
}
